<?php
	// +----------------------------------------------------------------------
	// | 邮件验证器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------
	namespace app\validate\ucenter;

	use app\common\validate\Base;
	use app\ucenter\model\Email as EmailModel;
	use app\ucenter\model\User as UserModel;
	use app\ucenter\model\EmailHistory as EmailHistoryModel;
	use think\captcha\Captcha;

	class Email extends Base
	{

		// 当日单个邮箱地址每天发送数量限制
		protected static $day_limit = 0;
		// 两次发送间隔
		protected static $send_between = 60;
		// ip每天发送次数
		protected static $ip_max = 10;

		// ip白名单
		protected static $ip_white_list = ['127.0.0.1'];
		//ip 黑名单
		protected static $ip_black_list = [];
		protected static $open_token = false;


		/* 用户模型自动验证 */
		protected $rule = [
			'address' => [
				'email',
				'require',
//				'checkEmail',
			],
		];

		protected $message = [
			'address.email' => '您填写的邮箱格式不正确！',
			'address.require' => '您需要填写您的邮箱！',
		];

		/**
		 * 发送场景
		 * @return Email
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function sceneSend()
		{
			return $this->append('email', 'checkEmail:send');
		}

		/**
		 * 添加订阅场景
		 * @return Email
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function sceneAdd()
		{
			return $this->append('email', 'checkEmail:add');
		}


		/**
		 * 添加订阅场景
		 * @return Email
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function sceneEdit()
		{
			return $this->append('email', 'checkEmail:edit');
		}


		/**
		 * 发送手机安全验证
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return string
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/6/23 8:34
		 */
		protected function checkEmail($value, $rule, $data)
		{
			if ('add' == $rule) {
				// 添加订阅需要唯一
				return EmailModel::where('address', $value)->where('status', 1)->find() ? true : '当前地址已经订阅';
			}elseif ('edit' == $rule) {
				// 添加订阅需要唯一
				return EmailModel::where('address', $value)->where('status', 1)->find() ? true : '当前地址已经订阅';
			} else {

				if (self::$open_token && false == $this->token($data['__token__'], '__token__', $data)) {
					return '页面超时，请刷新后再试';
				}

				$email = EmailModel::where('address', $value)->find();
				if ($email) {
					// 检验当日发送次数
					if (self::$day_limit > 0 && $email->historys()->whereTime('create_time', 'today')->where('status', 1)->count('*') > self::$day_limit) {
						return '当天邮件频率超过上限';
					}
					//检查上次发送
					if (self::$send_between > 0 && $email->historys()->whereTime('create_time', '>', time() - self::$send_between)->find()) {
						return '请'.self::$send_between.'秒后再试';
					}

				}
				if (in_array($data['type'], ['register', 'forget', 'bind','register2'])) {
					// 需要填写验证码
//					if (in_array($data['type'], ['register', 'forget','register2']) && !captcha_check($data['captcha'])) {
//						// 验证失败
//						return '验证码填写错误';
//					}
					if ('bind' == $data['type']) {
						if (!$this->request->user) {
							return '请登录后再试';
						}
						// 需要无人使用
						if (UserModel::where('email', $value)->where('id', '<>', $this->request->user['id'])->where('status', '>', -2)->find()) {
							return '当前邮箱已经注册';
						}
					} else {

						switch ($data['type']) {
							case 'forget':
								// 待使用
								if (!UserModel::where('email', $value)->where('status', '>', -2)->find()) {
									return '当前邮箱没有注册';
								}
								break;
							default:
								// 需要无人使用
								if (UserModel::where('email', $value)->where('status', '>', -2)->find()) {
									return '当前邮箱已经注册';
								}
								break;
						}
					}
				}
				// 短信黑白名单
				if (in_array($this->request->ip(), self::$ip_black_list)) {
					return '当前IP已禁用';
				}
				if (in_array($this->request->ip(), self::$ip_white_list)) {
					return true;
				}
				// 检查ip发送频率
				if (self::$ip_max > 0 && EmailHistoryModel::where('ip', $this->request->ip())->whereTime('create_time', 'd')->count('*') > 0) {
					return '当日IP达到发送上限';
				}
				return true;
			}
		}

		/**
		 * 验证码检查
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return bool|string
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/2/16 18:44
		 */
		protected function checkCaptcha($value, $rule, $data)
		{
			if (captcha_check($value)) {
				return true;
			} else {
				return '验证码错误';
			}
		}
	}
